package com.data_structure_algorithm.data_structure.tree.avl;

/**
 * @Authror PABLO
 * @Date 2022/4/3 22:45
 * @Desc
 */
public class AVLTree {
    public static void main(String[] args) {
        int[] arr = {10, 11, 7, 6,6,6,6,6,6,6, 8, 9};
        AVL avl = new AVL();

        for (int i = 0; i < arr.length; i++) {
            avl.add(new Node(arr[i]));
        }

        avl.infixOrder();
        System.out.println("当前根节点的树的高度" + avl.getRoot().getCurTreeHeight());
        System.out.println("当前根节点的左子树高度" + avl.getRoot().leftHeight());
        System.out.println("当前根节点的右子树高度" + avl.getRoot().rightHeight());
    }


}

class AVL {
    private Node root;//根节点

    public Node getRoot() {
        return root;
    }

    /**
     * @Description:查找待删除节点
     * @Author: PABLO
     * @Date: 2022/4/3 22:01
     * @Params: [value]
     * @Return: com.learn.binarytree.Node
     **/
    public Node search(int value) {

        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    /**
     * @Description:找到当前节点为树根下的最小节点权值
     * @Author: PABLO
     * @Date: 2022/4/3 22:01
     * @Params: [node]
     * @Return: int
     **/
    public int delRightTreeMin(Node node) {

        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    /**
     * @Description:根据value删除节点
     * @Author: PABLO
     * @Date: 2022/4/3 22:02
     * @Params: [value]
     * @Return: void
     **/
    public void delNode(int value) {

        if (root == null) {
            return;
        } else {
            //获取待删除的节点
            Node targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            //要删除节点是根节点
            if (root.left == null && root.right == null && root.value == value) {
                root = null;
                return;
            }
            //获取待删除节点的父节点
            Node parent = searchParent(value);
            //情况一  待删除节点是叶子结点
            if (targetNode.left == null && targetNode.right == null) {
                //待删除节点是父节点的左节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                    //待删除节点是父节点的右节点
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
                //情况2 带删除节点有左右两颗子树
            } else if (targetNode.left != null && targetNode.right != null) {
                //查询待删除节点右侧最小节点，即右侧节点一直向左延伸（或左侧最大节点，左侧节点一直向右延伸）
                int i = delRightTreeMin(targetNode.right);
                //将得到的最小值替换为待删除节点的value，符合二叉排序树的特点
                targetNode.value = i;
                //情况3： 待删除节点有一颗子树
            } else {
                if (targetNode.left != null) { //待删除节点有左节点
                    if (parent != null) {//如果待删除节点不是根节点
                        //判断待删除节点和带删除节点父节点的关系
                        if (parent.left.value == value) {//根节点的左子节点建立关联
                            parent.left = targetNode.left;
                        } else {//根节点的右子节点建立关联
                            parent.right = targetNode.left;
                        }
                    } else {//待删除节点为根节点，直接赋值即可，说明此时树中只有两个节点
                        root = targetNode.left;
                    }
                } else {
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else if (parent.right.value == value) {
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    //查询当前节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //添加节点
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("");
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    /**
     * @Description: 获取左子树高度
     * @Author: PABLO
     * @Date: 2022/4/3 22:53
     * @Params: []
     * @Return: int
     **/
    public int leftHeight() {
        if (this.left == null) return 0;
        return this.left.getCurTreeHeight();
    }

    /**
     * @Description: 获取右子树高度
     * @Author: PABLO
     * @Date: 2022/4/3 22:53
     * @Params: []
     * @Return: int
     **/
    public int rightHeight() {
        if (this.right == null) return 0;
        return this.right.getCurTreeHeight();
    }


    /**
     * @Description: 返回当前节点为根节点的高度 左右最大高度
     * @Author: PABLO
     * @Date: 2022/4/3 22:46
     * @Params:
     * @Return: int
     **/
    public int getCurTreeHeight() {
        return Math.max(this.left == null ? 0 : this.left.getCurTreeHeight(),
                this.right == null ? 0 : this.right.getCurTreeHeight()) + 1;
    }


    /**
     * @Description:查询找待删除节点
     * @Author: PABLO
     * @Date: 2022/4/3 22:00
     * @Params: [value]
     * @Return: com.learn.binarytree.Node
     **/
    public Node search(int value) {

        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    //查询父节点
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }

    //添加节点
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
        //判断右子树高度是否大于左子树>1
        if (this.rightHeight() - this.leftHeight() > 1) {
            //双旋转
            if (this.right != null && this.right.leftHeight() > this.right.rightHeight()) {
                this.right.rightRotation();
            }
            leftRotation();
            return;
        }
        //左子树的高度大于右子树高度>1   右旋转
        if (this.leftHeight() - this.rightHeight() > 1) {
            //局部判断，左子树的右子树高度大于左子树的左子树高度>1
            //双旋转
            if (this.left != null && this.left.rightHeight() > this.left.leftHeight()) {
                //局部左旋转
                this.left.leftRotation();
            }
            //外围右旋转
            rightRotation();

        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * @Description:左旋转 调用时机：添加时候判断 右子树高度 左子树高度 >1层
     * @Author: PABLO
     * @Date: 2022/4/3 23:07
     * @Params: [root]
     * @Return: void
     **/
    public void leftRotation() {

        Node temp = new Node(this.value);

        temp.left = this.left;

        temp.right = this.right.left;

        this.value = this.right.value;

        this.right = this.right.right;

        this.left = temp;

    }

    /**
     * @Description:右旋转 调用时机：添加时候判断 左子树高度 右子树高度  >1层
     * @Author: PABLO
     * @Date: 2022/4/3 23:07
     * @Params: [root]
     * @Return: void
     **/
    public void rightRotation() {

        Node temp = new Node(this.value);

        temp.right = this.right;

        temp.left = this.left.right;

        this.value = this.left.value;

        this.left = this.left.left;

        this.right = temp;


    }

}
